//app.js
App({
  onLaunch: function() {
    wx.request({
      url:"https://api.geek51.cn/commons/online/",
      success: res => {
        wx.setStorageSync('allPeople', res.data.data.online_num);
      }
    });
    var that=this;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    wx.setStorageSync('tabBar', that.globalData.tabBar);
    /*wx.hideTabBar();*/
    //版本更新
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function(res) {
        console.log('onCheckForUpdate====', res)
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          wx.showModal({
            title: '更新提示',
            content: '新版本它来了，快点更新吧！',
            showCancel: false,
            success: function(res) {
              console.log('success====', res)
              // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        }
      })
    }
    wx.showShareMenu({
      withShareTicket: true
    })
   
    wx.login({
      success: function(res) {
        wx.request({
          url: that.globalData.targetUrl + 'wx/login/',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            code: res.code
          },
          success: function(res) {
            var user_id = res.data.data.user_id;
            wx.setStorageSync('user_id', user_id); //将user_id设置缓存到本地
            var is_Register = true;
            var error = res.data.error;
            var is_campus = 1;
            if (error == "500") {
              is_Register = false;
              console.log("No Rregister!"); //进入首页，判断是否完成注册
            } else {
              console.log("Rregister!");
            }
            that.getAllSkin();
            that.getNowSkin();
            wx.setStorageSync('openid', res.data.data.openid); //将所需要存的字段 存入本地缓存中
            wx.setStorageSync('is_campus', is_campus);
            wx.setStorageSync('is_Register', is_Register);
          }
        })
      }
    });
  },

  globalData: {
    targetUrl: "https://api.geek51.cn/", //服务器接口
    imageUrl: "http://static.geek51.cn/", //七牛云图片路径
    localUrl: "http://10.205.50.177:8000/", //本地测试路径
    netUrl: "https://net.geek51.cn/",   //校园网接口
    nowSkinUrl: "",
    userInfo: null,
    isReturn:false,
    student_error: 201,
    nowColor:"#fff"
    /*tabBar: {
      "color": "#fff",
      "selectedColor": "#f4d155",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "首页"
        },
        {
          "pagePath": "/pages/list/list",
          "text": "旧物"
        },
        {
          "pagePath": "/pages/lifeCircle/lifeCircle",
          "text": "话题"
        },
        {
          "pagePath": "/pages/chatlist/chatList",
          "text": "消息"
        },
        {
          "pagePath": "/pages/my/my",
          "text": "我的"
        }
      ]
    }*/
  },
  /*editTabBar: function () {
    let tabbar = wx.getStorageSync('tabBar');
    console.log(tabbar);
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    if (pagePath.indexOf('/') != 0) pagePath = '/' + pagePath;
    console.log(pagePath)
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      console.log(tabbar.list[i].pagePath)
      // console.log(tabbar.list[i].pagePath)
      // console.log("/pages/publish/publish")
      if (tabbar.list[i].pagePath == pagePath) {
        tabbar.list[i].selected = true;
        console.log(pagePath)
      }

    }
    _this.setData({
      tabbar: tabbar
    });
  },*/
  globalSkinUrl: [],
  getAllSkin: function() {
    var that = this;
    wx.request({
      url: that.globalData.targetUrl + "commons/wallpaper/",
      method: "get",
      success: res => {
        that.globalSkinUrl = res.data.data;
        for(var i = 0; i < that.globalSkinUrl.length%3; i++){
          that.globalSkinUrl.push(null);
        }
      }
    })
  },
  getNowSkin: function() {
    var that = this;
    var user_id = wx.getStorageSync("user_id");
    wx.request({
      url: that.globalData.targetUrl + "users/" + user_id + "/",
      method: "get",
      success: res => {     
        that.globalData.nowSkinUrl = res.data.data.wallpaper_url;
        var index = res.data.data.wallpaper-1;
        that.globalData.nowColor = "#fff";
        /*
        console.log(index)
        console.log(that.globalSkinUrl[index])
        if(that.globalSkinUrl[index].is_dark == 0){
          
          that.globalData.nowColor = "#0086ff";
        }else{
          that.globalData.nowColor = "#fff";
        }*/
      }
    })
  },
  saveFormId: function(userId, formId) {
    wx.request({
      url: 'https://api.geek51.cn/wx/form_id/',
      method: 'post',
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'user_id': userId,
        'form_id': formId
      },
      success: res => {
        console.log(res.data);
      }
    })
  },
  
})